package ru.spbu.math.seabattle.client.jms.gui;

import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import ru.spbu.math.seabattle.client.jms.SeaBattleClient;
import ru.spbu.math.seabattle.entity.PlayerID;

public class WaitingForReplyGUI extends JFrame {

	private static final long serialVersionUID = 1L;

	private final Rectangle frameRectangle = new Rectangle(200, 200, 200, 100);
	
	private final JButton cancelButton = new JButton("Cancel");
	private final Rectangle buttonRectangle = new Rectangle(50, 40, 100, 25);
	
	private final JLabel waitingForReplyLabel;
	private final Rectangle labelRectangle = new Rectangle(20, 10, 160, 20);
	
	public WaitingForReplyGUI(final SeaBattleClient client, String opponentName, final PlayerID opponentId, int x, int y) {
	
		this.setBounds(frameRectangle);
		this.setResizable(false);
		this.setVisible(true);

		frameRectangle.x = x;
		frameRectangle.y = y;
		
		waitingForReplyLabel = new JLabel("Waiting for reply from " + opponentName);
		
		this.getContentPane().setLayout(null);
		
		waitingForReplyLabel.setBounds(labelRectangle);
		cancelButton.setBounds(buttonRectangle);
		
		this.add(cancelButton);
		this.add(waitingForReplyLabel);
		
		cancelButton.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent arg0) {
				client.onPlayerSelectionCanceled(opponentId);
			}
			
			@Override public void mouseEntered(MouseEvent arg0) {}
			@Override public void mouseExited(MouseEvent arg0) {}
			@Override public void mousePressed(MouseEvent arg0) {}
			@Override public void mouseReleased(MouseEvent arg0) {}
		});
		
		this.addWindowListener(new WindowListener() {

			@Override
			public void windowClosing(WindowEvent arg0) {
				cancelButton.doClick();			
			}
			
			@Override public void windowActivated(WindowEvent arg0) {}
			@Override public void windowClosed(WindowEvent arg0) {}
			@Override public void windowDeactivated(WindowEvent arg0) {}
			@Override public void windowDeiconified(WindowEvent arg0) {}
			@Override public void windowIconified(WindowEvent arg0) {}
			@Override public void windowOpened(WindowEvent arg0) {}
		});
	}
}
